package com.shanghaionstar.utils;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.onstar.cn.common.log.LogManager;
import com.shanghaionstar.OnstarApplication;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Yonglang Hong on 2016/10/24.
 */
public class WebViewSettingUtil {

    public static final String TAG = "WebViewSettingUtil";

    public static void setupDefaultSettings(WebView web_view) {
        // WebSettingManager wsm = WebSettingManager.getInstance();
        WebSettings settings = web_view.getSettings();
        // settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // settings.setCacheMode(WebSettings.LOAD_NORMAL);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        settings.setLightTouchEnabled(false);
        settings.setNeedInitialFocus(false);

        // settings.setLoadsImagesAutomatically(wsm.getIsLoadImage());
        settings.setJavaScriptEnabled(true);

        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        //5.0前默认是MIXED_CONTENT_ALWAYS_ALLOW，5.0后默认MIXED_CONTENT_NEVER_ALLOW,需要手动设置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            settings.setDisplayZoomControls(false);
        } else {

        }
        settings.setDefaultTextEncodingName("GBK");
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);

        settings.setRenderPriority(WebSettings.RenderPriority.NORMAL);

        settings.setGeolocationEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheEnabled(true);

        Context context = OnstarApplication.getApplication();
        // long appCacheMaxSize =
        // UmeBrowserActivity.myself.getWebStorageSizeManagerInstance().getAppCacheMaxSize();
        String databasePath = context.getDir("databases", 0).getPath();
        String geolocationDatabasePath = context.getDir("geolocation", 0).getPath();
        String appCachePath = context.getDir("appcache", 0).getPath();

        settings.setGeolocationDatabasePath(geolocationDatabasePath);
        settings.setDatabasePath(databasePath);
        // settings.setAppCacheMaxSize(appCacheMaxSize);
        settings.setAppCachePath(appCachePath);
        settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        settings.setSavePassword(true);
        settings.setSaveFormData(true);

        // settings.setPageCacheCapacity(10);
        reflectPageCacheTo(settings, 20);
        // settings.setUserAgentString(wsm.getUAString());

        settings.setSupportMultipleWindows(true);

        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        // reflectPrivateBrowsingEnable(settings,
        // wsm.getPrivateBrowsingEnable());

        // settings.setTextSize(mapIntToTextSize(wsm.getTextSize()));
        // mapTextSizeToZoom(webview, mapIntToTextSize(wsm.getTextSize()));

        // Set the nav dump for HTC 2.x devices (disabling for ICS, deprecated
        // entirely for Jellybean 4.2)
        try {
            Method gingerbread_getMethod = WebSettings.class.getMethod("setNavDump", boolean.class);

            String manufacturer = android.os.Build.MANUFACTURER;
            Log.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB
                    && android.os.Build.MANUFACTURER.contains("HTC")) {
                gingerbread_getMethod.invoke(settings, true);
            }
        } catch (NoSuchMethodException e) {
            Log.d(TAG, "We are on a modern version of Android, we will deprecate HTC 2.3 devices in 2.8");
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "Doing the NavDump failed with bad arguments");
        } catch (IllegalAccessException e) {
            Log.d(TAG, "This should never happen: IllegalAccessException means this isn't Android anymore");
        } catch (InvocationTargetException e) {
            Log.d(TAG, "This should never happen: InvocationTargetException means this isn't Android anymore.");
        }
    }

    private static void reflectPageCacheTo(WebSettings settings, int capacity) {
        try {
            Method setpgcache = settings.getClass().getDeclaredMethod("setPageCacheCapacity", int.class);
            setpgcache.setAccessible(true);
            setpgcache.invoke(settings, capacity);
        } catch (NoSuchMethodException e) {
            LogManager.getInstance().e(e.getMessage());

        } catch (IllegalArgumentException e) {
            LogManager.getInstance().e(e.getMessage());

        } catch (IllegalAccessException e) {
            LogManager.getInstance().e(e.getMessage());

        } catch (InvocationTargetException e) {
            LogManager.getInstance().e(e.getMessage());

        }
    }

}
